var util = require('../../../../utils/util.js');
var api = require('../../../../config/api.js');
import Notify from '../../../../lib/vant-dist/notify/notify';

Page({

    /**
     * 页面的初始数据
     */
    data: {
        option: [{
                text: '请选择银行',
                value: 0
            },
            {
                text: '中国银行',
                value: 1
            },
            {
                text: '中国农业银行',
                value: 2
            },
            {
                text: '中国建设银行',
                value: 3
            },
            {
                text: '中国工商银行',
                value: 4
            },
        ],
        value: 0,
        checked: false, //默认银行卡
        cardInfo: {}
    },
    // 需要手动对 checked 状态进行更新,设置默认银行卡
    onChange({
        detail
    }) {
        let that = this
        this.setData({
            checked: detail
        });
        if (this.data.checked) {
            that.data.cardInfo.isdefault = 1
        } else {
            that.data.cardInfo.isdefault = 0
        }
        this.setData({
            cardInfo: that.data.cardInfo
        })
    },
    //银行卡号取消焦点
    changeYHKH(event) {
        let that = this
        that.data.cardInfo.yhkkh = event.detail.value
        this.setData({
            cardInfo: that.data.cardInfo
        })
    },
    //开户行选择完毕
    changeYHKtype(value) {
        let that = this
        switch (value.detail) {
            case 1:
                that.data.cardInfo.yhktype = '中国银行'
                cardInfo: that.cardInfo
                break;
            case 2:
                that.data.cardInfo.yhktype = '中国农业银行'
                cardInfo: that.cardInfo
                break;
            case 3:
                that.data.cardInfo.yhktype = '中国建设银行'
                cardInfo: that.cardInfo
                break;
            case 4:
                that.data.cardInfo.yhktype = '中国工商银行'
                cardInfo: that.cardInfo
                break;
            default:
                break;
        }
    },
    //保存
    preservation() {
        let data = {
            id: this.data.cardInfo.id,
            userid: wx.getStorageSync('userid'),
            yhkkh: this.data.cardInfo.yhkkh,
            yhktype: this.data.cardInfo.yhktype,
            isdelete: 0,
            isdefault: this.data.cardInfo.isdefault
        }
        if (data.yhkkh && data.yhktype) {
            util.request(api.BankCardUpdate, data, 'post').then((res) => {
                if (res.errmsg == '成功') {
                    wx.navigateBack()
                } else {
                    wx.showToast({
                        title: '保存失败',
                        icon: 'error',
                    })
                }
            })
            if (data.isdefault == 1) {
                util.request(api.BankCardSetDefault + `?id=${data.id}`).then(res => {})
            }
        } else {
            Notify({
                type: 'warning',
                message: '请正确填写银行卡信息'
            });
        }

    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        util.request(api.BankCardDetail, options).then((res) => {
            this.setData({
                cardInfo: res.data
            })
            switch (res.data.yhktype) {
                case '中国银行':
                    this.setData({
                        value: 1
                    })
                    break;
                case '中国农业银行':
                    this.setData({
                        value: 2
                    })
                    break;
                case '中国建设银行':
                    this.setData({
                        value: 3
                    })
                    break;
                case '中国工商银行':
                    this.setData({
                        value: 4
                    })
                    break;
                default:
                    this.setData({
                        value: 0
                    })
                    break;
            }
            if (res.data.isdefault) {
                this.setData({
                    checked: true
                })
            }
        })
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

})